﻿using System;
using System.ComponentModel;

namespace Softlanding.ComponentModel
{
    /// <summary>
    /// Specifies the localized name of the category in which to group the property or event
    /// when displayed in a PropertyGrid control set to Categorized mode.
    /// </summary>
    [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    public class SLCategoryAttribute : CategoryAttribute
    {
        private readonly string _classKey;

        /// <summary>
        /// Initializes a new instance of the SLCategoryAttribute class
        /// </summary>
        /// <param name="classKey">The base file name of the language resource file containing a localized category name</param>
        /// <param name="resourceKey">The name half of a name/value pair in a resource file</param>
        public SLCategoryAttribute(string classKey, string resourceKey)
            : base(resourceKey)
        {
            _classKey = classKey;
        }

        /// <summary>
        /// Retrieves the localized name of the specified category
        /// </summary>
        /// <param name="resourceKey">The name half of a name/value pair in a resource file</param>
        protected override string GetLocalizedString(string resourceKey)
        {
            var value = CommonHelper.GetResourceString(_classKey, resourceKey);
            if (String.IsNullOrEmpty(value))
            {
                return base.GetLocalizedString(resourceKey);
            }
            else
            {
                return value;
            }
        }
    }
}